package top.boking.client;

import io.socket.client.IO;
import io.socket.client.Socket;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URISyntaxException;
import java.util.Random;

public class SocketIOClient {
    private Socket socket;
    private String roomId;

    public SocketIOClient() {
        // 生成随机房间ID
        this.roomId = "room_" + generateRandomString();
        
        try {
            // 连接到服务器
            IO.Options options = new IO.Options();
            options.forceNew = true;
            options.reconnection = true;
            options.reconnectionAttempts = 10;
            options.timeout = 10000;
            options.transports = new String[]{"websocket", "polling"};  // 添加这行
            socket = IO.socket("http://localhost:30000", options);
            
            // 设置事件监听器
            setupEventListeners();
            
            // 连接到服务器
            socket.connect();
            
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    private void setupEventListeners() {
        // 连接成功事件
        socket.on(Socket.EVENT_CONNECT, args -> {
            System.out.println("已连接到服务器！socket.id: " + socket.id());
            // 加入房间
            socket.emit("join-room",  new String[]{roomId}, (Object... ack) -> {
                if (ack.length > 0) {
                    System.out.println("成功加入房间: " + roomId);
                    System.out.println("我的ID: " + ack[0]);
                    System.out.println("当前房间状态: " + ack[1]);
                    
                    // 开始定期发送位置更新
                }
            });
        });
        startSendingUpdates();

        // 新用户加入事件
        socket.on("user-joined", args -> {
            String userId = (String) args[0];
            System.out.println("新用户加入: " + userId);
        });

        // 用户离开事件
        socket.on("user-leave", args -> {
            String userId = (String) args[0];
            System.out.println("用户离开: " + userId);
        });

        // 房间状态更新事件
        socket.on("room-state", args -> {
            JSONObject snapshot = (JSONObject) args[0];
            System.out.println("房间状态更新: " + snapshot.toString());
        });

        // 连接错误事件
        socket.on(Socket.EVENT_CONNECT_ERROR, args -> {
            System.out.println("连接错误: " + args[0]);
        });

        // 断开连接事件
        socket.on(Socket.EVENT_DISCONNECT, args -> {
            System.out.println("断开连接: " + args[0]);
        });
    }

    private void startSendingUpdates() {
        // 创建一个线程定期发送位置更新
        new Thread(() -> {
            Random random = new Random();
            while (!Thread.interrupted()) {
                try {
                    JSONObject state = new JSONObject();
                    state.put("x", random.nextDouble() * 100);
                    state.put("y", random.nextDouble() * 100);
                    state.put("z", random.nextDouble() * 100);
                    
                    socket.emit("update-state", state);
                    
                    Thread.sleep(1000); // 每秒更新一次
                } catch (InterruptedException | JSONException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        }).start();
    }

    private String generateRandomString() {
        return Long.toString(System.currentTimeMillis() % 1000000);
    }

    public void disconnect() {
        if (socket != null) {
            socket.disconnect();
        }
    }

    public static void main(String[] args) {
        SocketIOClient client = new SocketIOClient();
        
        // 添加关闭钩子
        Runtime.getRuntime().addShutdownHook(new Thread(client::disconnect));
    }
}